


export class prefabServices {
    PrefabCache: object = {};
    prefabArray: Array<string> = [];
    prefabPool: object = {
        // zuan: []
    }
    getPrefabPoolItem(name): cc.Node{
        let prefabNode = this.prefabPool[name].get();
        if(prefabNode == null){
            prefabNode = cc.instantiate(this.PrefabCache[name]);
            // prefabNode.setPosition(0, 0);
        }
        return prefabNode;
    }
    putPrefabPool(name, node){
       this.prefabPool[name].put(node);
    }
    initPrefab() {
       return new Promise((resolve, reject) => {
            let i = 1;
            this.prefabArray.forEach((val, index) => {
                cc.loader.loadRes("prefab/"+val,(error, res) => {
                    if(error){
                        reject(error) ;
                    }
                    i++;
                    this.PrefabCache[val] = res;
                    if(i > this.prefabArray.length){
                        this.initPrefabPool();
                        cc.log('预制体加载完毕');
                        resolve(true);
                    }
                })
            })
       })
    }
    initPrefabPool() {
            for(let key in this.prefabPool){
                this.prefabPool[key] = new cc.NodePool();
                for(let i = 0;i< 50;i++){
                    let foodPrefab = cc.instantiate(this.PrefabCache[key]);
                    this.prefabPool[key].put(foodPrefab);
                }
            }
    } 
    
}